import java.util.List;

public class TreeData2CellData {

    private TableData tableData = new TableData();

    public TableData treeData2CellData(List<TreeData> treeDatas) {
        int row = 0;
        for (TreeData treeData : treeDatas) {
            row = countRowNum(treeData, 0, row);
        }
        return tableData;
    }

    private int countRowNum(TreeData treeData, int colIndex, int rowIndex) {
        int count = 0;
        List<TreeData> treeDatachildren = treeData.getChildren();
        if (treeDatachildren != null) {
            int cCol = colIndex;
            int cRow = rowIndex;
            colIndex += 1;
            for (TreeData treeDatachild : treeDatachildren) {
                int cCount = countRowNum(treeDatachild, colIndex, rowIndex);
                count += cCount;
                rowIndex += count;
            }
            CellData cellData = new CellData();
            cellData.setValue(treeData.getContent());
            cellData.setPosCol(cCol);
            cellData.setPosRow(cRow);
            cellData.setRowSpan(count);
            tableData.addCell(cellData);
            return count;
        }
        CellData cellData = new CellData();
        cellData.setValue(treeData.getContent());
        cellData.setPosCol(colIndex);
        cellData.setPosRow(rowIndex);
        tableData.addCell(cellData);
        return 1;
    }

}
